package com.edooteach.api.web.controller.payment;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.demo2do.core.entity.result.HttpResult;
import com.edoomath.entity.trade.SalesOrder;
import com.edoomath.entity.user.User;
import com.edoomath.entity.user.UserCredit;
import com.edooteach.api.servcice.SalesOrderService;
import com.edooteach.api.servcice.UserService;

@Controller
@RequestMapping("/credit")
public class CreditApiController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private SalesOrderService salesOrderService;
    
    /**
     * 
     * @param unionid
     * @return
     */
    @RequestMapping(value = "/load", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult onCharge(@RequestParam("unionid") String unionid) {
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            
            if(user == null || user.getMobile().isEmpty()) {
                return httpResult.fail("400").message("用户信息异常");
            }
            
            UserCredit userCredit = userService.saveOrGetUserCredit(user);
            
            return httpResult.success().data("userCredit", userCredit).message("获取积分信息成功");
            
        } catch (Exception e) {
            return httpResult.fail("500").message("创建订单异常");
        }
    }
    
    /**
     * 
     * @param unionid
     * @param credits
     * @param money
     * @param request
     * @return
     */
    @RequestMapping(value = "/chanrge", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResult onCharge(@RequestParam("unionid") String unionid,
                               @RequestParam("credits") Integer credits,
                               @RequestParam("money") Integer money,
                               HttpServletRequest request) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            User user = userService.load(unionid);
            
            if(user == null || user.getMobile().isEmpty()) {
                return httpResult.fail("400").message("用户信息异常");
            }
            
            if(money == null || credits == null) {
                return httpResult.fail("400").message("参数异常");
            }
            
            SalesOrder salesOrder = new SalesOrder(user, user.getMobile(), money, credits);
            salesOrderService.save(salesOrder);
            
            return httpResult.success().data("order", salesOrder).message("订单创建成功");
            
        } catch (Exception e) {
            return httpResult.fail("500").message("创建订单异常");
        }
    }
    
    
}
